#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(){

//	int pipe(int fd[2]);   
//	返回值：若成功返回0，失败返回-1
	int fd[2];
	pid_t pid;
	char buf[128];

	if(pipe(fd) < 0)
		printf("creat pipe error!\n");

	pid = fork();
	if(pid < 0){
		printf("creat pid error!\n");
	}
	else if(pid > 0){
	
		wait(0);
		sleep(2);
		close(fd[1]);
		printf("this is father pid\n");
		int n_read = read(fd[0],buf,sizeof(buf)-1);
		printf("read %d btye from chlid pid\ncontents :%s\n",n_read,buf);
	}
	else{
		close(fd[0]);
		printf("this is child pid\n");
		int n_write = write(fd[1],"Hello father pid! massege from child\n",strlen("Hello father pid! massege from child")+1);
		printf("child write %d byte to father\n",n_write);
		exit(0);
	}
}
